<!-- 绑定弹窗 -->

<template>
  <div>
    <FormRadio
      label="地图继承"
      bind="mapParent"
      :dict="DICT.MAP_INHERIT"
      :disabled="!validParent(one.areaId)"
      @change="changeInherit"
    />

    <FormTree :label="`当前${CORE.MAP.REGION}`" bind="areaId, areaName" :dict="CORE.REGION.DICT" allName disabled />

    <FormInput v-show="false" bind="parentId" />

    <FormSelect
      :label="`继承${CORE.MAP.REGION}`"
      bind="parentAreaId, parentAreaName"
      :dict="regionSelect"
      :disabled="!isInherit"
      required
    />
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

const KEY_LIST = [
  'mapType',
  'fileUrl',
  'fileName',
  'mapName',
  'mapRate',
  'rateBefore',
  'rateAfter',
]

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyInherit',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb('bind'), CORE.mixInfo('bind')],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 是否为继承
     */
    isInherit() {
      return this.one.mapParent === 1
    },

    /**
     * 区域下拉
     */
    regionSelect() {
      let { one: { areaId } } = this
      const select = []

      while (this.validParent(areaId)) {
        const { parentId } = CORE.REGION.MAP[areaId]
        select.push([parentId, CORE.REGION.NAME[parentId]])
        areaId = parentId
      }

      return select
    },
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听信息就绪
     */
    readyInfo(ready) {
      ready && this.changeRegion()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 所传 ID 的父区域是否有效
     */
    validParent(areaId) {
      const { parentId } = CORE.REGION.MAP[areaId] || {}
      return parentId && parentId !== '0' && CORE.REGION.MAP[parentId]
    },

    /**
     * 切换继承
     */
    changeInherit() {
      if (this.isInherit) KEY_LIST.forEach((key) => (this.one[key] = this.INIT_ONE[key]))

      this.one.parentId = ''
      this.one.parentAreaId = undefined
      this.one.parentAreaName = ''

      this.refForm.check()
    },

    /**
     * 切换区域
     */
    changeRegion() {
      this.one.parentId = this.info.id || ''
    },
  },
}
</script>
